package org.lex.swt.table;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

public class TableUtils {
	public static void pack(Table table) {
		for (int i = 0, n = table.getColumnCount(); i < n; i++) {
			table.getColumn(i).pack();
		}
	}

	public static TableViewer createTable(Composite parent, int style, String[] columnNames) {
		TableViewer viewer = new TableViewer(parent, style);

		Table table = viewer.getTable();
		for (String columnName : columnNames) {
			TableColumn column = new TableColumn(table, SWT.LEFT);
			column.setText(columnName);
			column.setResizable(true);
			column.setToolTipText(columnName);
			column.pack();
		}

		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		viewer.setContentProvider(new ArrayContentProvider());
		viewer.setLabelProvider(new RowLabelProvider());
		return viewer;
	}
}
